package com.pizzaonline.JDBC;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DataBase {

	private DataBaseData data;

	private String url = "";

	private Connection connexion = null;

	private Statement statement = null;
	
	public void connexionBD()
	{
		try
		{
			File f=new File("config.java");
			String serveur="localhost";
			String utilisateur="root";
			String mdp="";
			if (f.exists())
			{
				BufferedReader fr=new BufferedReader(new FileReader(f));
				for (int i=0;i<5;i++)
				{
					String line=fr.readLine();
					switch (i)
					{
					case 0:
					case 4:
						break;
					case 1:
						serveur=line;
						break;
					case 2:
						utilisateur=line;
						break;
					case 3:
						mdp=line;
						break;
					default:
						break;

					}
				}
			}
			Class.forName("com.mysql.jdbc.Driver").newInstance();

			connexion = DriverManager.getConnection("jdbc:mysql://"+serveur+":3306/gii",utilisateur,mdp);
			
			statement = connexion.createStatement();
		}
		catch (Exception e)
		{
			System.out.println(e.toString());
		}


	}

//	public void connect() throws Exception {
//
//		try
//
//		{
//
//			// enregister le driver JDBC de MYSQL
//
//			Class.forName("com.mysql.jdbc.Driver");
//
//			url = "jdbc:mysql://" + data.getHostName()+":"+data.getPort() + "/" + data.getDatabaseName();
//
//			//url = "jdbc:mysql://localhost:3306/gii";
//
//			connexion = DriverManager.getConnection(url, data.getUserName(), data.getPassword());
//
//			statement = connexion.createStatement();
//
//		}
//
//		catch (Exception e)
//
//		{
//
//			throw new Exception("Echec de connexion à la Base MYSQL", e);
//
//		}
//
//	}
	

	public Statement getStatement() {
		return statement;
	}



	public Connection getConnection() {
		return connexion;
	}



	public void setConnection(Connection connect) {
		this.connexion = connect;
	}
	
	public void closeConnection() throws SQLException {
		connexion.close();
	}



	public DataBaseData getData() {
		return data;
	}



	public void setData(DataBaseData data) {
		this.data = data;
	}



	public void setStatement(Statement stat) {
		this.statement = stat;
	}

	

	public ResultSet query(String query) throws SQLException {
		return statement.executeQuery(query);
	}



	public ResultSet select(String in) throws SQLException {
		return statement.executeQuery("SELECT " +in+ ";");
	}

	

	public ResultSet select(String in, String from) throws SQLException {
		return statement.executeQuery("SELECT " +in+ " FROM " +from+ ";");
	}

	

	public ResultSet select(String in, String from, String where) throws SQLException {
		return statement.executeQuery("SELECT " +in+ " FROM " +from+ " WHERE " +where+ ";");
	}

	

	public ResultSet selectOrdered(String in, String from, String where, String order) throws SQLException {
		return statement.executeQuery("SELECT " +in+ " FROM " +from+ " WHERE " +where+ " ORDER BY "+ order +";");
	}

	
	public boolean insert(String into, String columns, String values) throws SQLException {
		return statement.execute("INSERT INTO " +into+ " (" +columns+ ") VALUES ( " +values+ ");");
	}

	public void update(String in, String set, String where) throws SQLException {
		statement.executeUpdate("UPDATE " +in+ " SET " +set+ " WHERE " +where+ ";");
	}

	

	public PreparedStatement updatePS(String in, String set, String where) throws SQLException {
		return connexion.prepareStatement("UPDATE " +in+ " SET " +set+ " WHERE " +where+ ";");
	}

	

	public void delete(String from, String where) throws SQLException {
		statement.executeUpdate("DELETE FROM " +from+ " WHERE " +where+ ";");
	}

}
